var app = getApp()
Page({
    data: {
        width: app.data.systemInfo.screenWidth,
        height: 300,
        startX: 0,
        startY: 0,
        canvas: '',
        ctx: '',
        signfile_filename: '',
        signfile: '',
        envInfo: tt.getEnvInfoSync()
    },

    onLoad(options) {
        var that = this
        const userinfo = tt.getStorageSync('userinfo')
        if (!userinfo) {
            app.wxlogin().then(function (res) {
                that.setData({
                    userinfo: res,
                    signfile_filename: options.signfile_filename || ''
                })
            })
        } else {
            that.setData({
                userinfo,
                signfile_filename: options.signfile_filename || ''
            })
        }
    },

    onReady() {
        const query = tt.createSelectorQuery()
        query.select('#myCanvas').fields({ node: true,size: true }).exec((res) => {
            const canvas = res[0].node
            const ctx = canvas.getContext('2d')
            canvas.width = this.data.width
            canvas.height = this.data.height
            ctx.lineCap = 'round'
            ctx.lineGap = 'round'
            ctx.lineJoin = 'round'
            ctx.lineWidth = 4
            this.setData({
                ctx: ctx,
                canvas: canvas
            })
        })
    },

    handleTouchStart(e) {
        this.setData({
            startX: e.touches[0].x,
            startY: e.touches[0].y,
        })
    },

    handleTouchMove(e) {
        // console.log(e.touches[0].x)
        // console.log(e.touches[0].y)
        this.data.ctx.moveTo(this.data.startX,this.data.startY)
        this.data.ctx.lineTo(e.touches[0].x,e.touches[0].y)
        this.data.ctx.stroke()
        this.data.ctx.lineWidth = 4
        this.data.ctx.lineCap = 'round'
        this.data.ctx.lineGap = 'round'
        this.data.ctx.lineJoin = 'round'
        this.setData({
            startX: e.touches[0].x,
            startY: e.touches[0].y,
        })
    },

    resign() {
        this.data.ctx.clearRect(0,0,this.data.width,this.data.height)
        this.data.ctx.beginPath()
        this.setData({
            startX: 0,
            startY: 0,
        })
    },

    signsave() {
        var that = this
        if (that.data.startX == 0 && that.data.startY == 0) {
            tt.showToast({
                icon: 'none',
                title: "签名不能为空",
            })
        } else {
            // canvas转换base64数据
            let timestamp = Date.parse(new Date());
            let base64IMG = that.data.canvas.toDataURL()
            // console.log(timestamp)
            // console.log(base64IMG)
            let filePath = `${that.data.envInfo.common.USER_DATA_PATH + '/' + timestamp}.png`
            tt.getFileSystemManager().writeFileSync(filePath,tt.base64ToArrayBuffer(base64IMG.replace(/^data:image\/\w+;base64,/,'')))
            that.data.imgPath_ttfile = `${that.data.envInfo.common.USER_DATA_PATH + '/' + timestamp}.png`
            // console.log(that.data.imgPath_ttfile)
            let file = that.data.imgPath_ttfile
            const exfile = that.data.signfile_filename
            const { uid } = that.data.userinfo
            const data = { uid,exfile }
            // console.log(file,data)
            app.api_upload('?ctype=signfile',file,data).then(function (res) {
                if (res) {
                    let pages = getCurrentPages()
                    let beforePage = pages[pages.length - 2]
                    beforePage.setData({
                        signfile_filename: res.filename,
                        signfile: res.filepath,
                    });
                    tt.navigateBack({
                        delta: 1
                    })
                }
            })
        }
    }
})